package com.sixkey.config.interceptorconfig;

/**
 * ClassName: LoginInterceptor
 * Package: com.sixkey.interceptorconfig
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2023/11/1 - 21:50
 * @Version: v1.0
 */

import cn.dev33.satoken.stp.StpUtil;
import com.sixkey.exception.ServiceException;
import com.sixkey.response.ResponseEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义配置请求拦截器，所有的请求都会先通过这里
 */
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 请求转发之前执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            throw new ServiceException(ResponseEnum.FAIL);
        }
        String sa_token = StpUtil.getTokenValue();
        if(!token.equalsIgnoreCase(sa_token)){
            throw new ServiceException(ResponseEnum.FAIL);
        }
        return true;
    }
}
